package com.tanguy.driver.utlis.extend

import android.app.Activity
import android.text.TextUtils
import com.tanguy.driver.App.Companion.app
import com.tanguy.driver.bean.DriverRegister
import com.tanguy.driver.utlis.ToastUtils.showToast
import java.util.regex.Pattern


/**
 * Created by dab on 2017/11/3 0003 08:56
 * 检测是否符合规矩
 */
/**
 * 检测手机号
 *
 * @param phone
 * @return
 */
fun checkPhoneNumber(phone: String): Boolean {
    if (TextUtils.isEmpty(phone)) {
        showToast(app, "请输入手机号")
        return false
    }
    if (phone.length != 11) {
        showToast(app, "请输入11位的手机号")
        return false
    }
    if (!isPhoneNumber(phone)) {
        showToast(app, "手机号不合法")
        return false
    }
    return true
}

fun isPhoneNumber(phone: String): Boolean {
    val regexp = "((15[0-9])|(17[3678])|(18[0-9])|(14[57])|(13[0-9]))[0-9]{8}"
    val regex = Pattern.compile(regexp)
    val matcher = regex.matcher(phone)
    return matcher.find()
}
/**
 * 检测密码
 *
 * @param password
 * @return
 */
fun checkPassword(password: String): Boolean {
    if (TextUtils.isEmpty(password)) {
        showToast(app, "请输入密码")
        return false
    }
    val length = password.length
    if (length < 6 || length > 18) {
        showToast(app, "密码的长度在6到18位之间")
        return false
    }
    return true
}

/**
 * 检测两个密码是否一样
 *
 * @param password
 * @return
 */
fun Activity.checkPasswordAlike(password: String, newPassword: String): Boolean {
    if (!checkPassword(password)) {
        return false
    }
    if (TextUtils.isEmpty(newPassword)) {
        showToast("请输入确认密码")
        return false
    }
    if (password != newPassword) {
        showToast("两次密码不一致")
        return false
    }
    return true
}

/**
 * 检测验证码
 *
 * @param captcha
 * @return
 */
fun checkCaptcha(captcha: String): Boolean {
    if (TextUtils.isEmpty(captcha)) {
        showToast(app, "请输入验证码")
        return false
    }
    if (captcha.length != 6) {
        showToast(app, "请输入6位数的验证码")
        return false
    }
    return true
}


fun Activity.checkCardId(card: String): Boolean {
    if (TextUtils.isEmpty(card)) {
        showToast("请输入身份证号")
        return false
    }
    if (card.length < 15) {
        showToast("请输入正确的身份证号")
        return false
    }
    val substring = card.substring(0, card.length - 1)
    if (substring.toLowerCase().contains("x")) {
        showToast("请输入正确的身份证号")
        return false
    }
    return true
}




/**
 *检测司机注册信息是否填完了
 */
fun Activity.checkDriverRegister(driverRegister: DriverRegister): Boolean {
    if (TextUtils.isEmpty(driverRegister.name)) {
        showToast("请输入姓名")
        return false
    }
    if (driverRegister.sex == 0) {
        showToast("请选择性别")
        return false
    }
    if (!checkCardId(driverRegister.idcard)) {
        return false
    }
    if (TextUtils.isEmpty(driverRegister.drivage)) {
        showToast("请选择驾龄")
        return false
    }
    if (TextUtils.isEmpty(driverRegister.licence)) {
        showToast("请选择驾驶证")
        return false
    }
    if (TextUtils.isEmpty(driverRegister.modelId)) {
        showToast("请选择车辆类型")
        return false
    }
    if (TextUtils.isEmpty(driverRegister.color)) {
        showToast("请选择车辆颜色")
        return false
    }
    if (TextUtils.isEmpty(driverRegister.plateNumber)) {
        showToast("请输入车牌号")
        return false
    }
    if (TextUtils.isEmpty(driverRegister.plateArea)) {
        showToast("请选择车牌号")
        return false
    }
//    if (TextUtils.isEmpty(driverRegister.seat)) {
//        showToast("请选择座位数")
//        return false
//    }
    if (TextUtils.isEmpty(driverRegister.careful)) {
        showToast("请选择年审时间")
        return false
    }
    if (TextUtils.isEmpty(driverRegister.travel)) {
        showToast("请选择行驶证")
        return false
    }
    if (TextUtils.isEmpty(driverRegister.insurance)) {
        showToast("请选择保险")
        return false
    }
    if (TextUtils.isEmpty(driverRegister.peoplecar)) {
        showToast("请选择人车合影")
        return false
    }

    return true
}
